package com.share51.novel.security.interceptor;

import com.share51.novel.common.ViewConstant;
import com.share51.novel.config.NovelConfig;
import com.share51.novel.security.annotation.SetCommonInfo;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 通用内容拦截器 主要用于页面请求返回的时候 携带通用的内容（）
 * @author gb
 */
@Component
public class CommonInterceptor extends HandlerInterceptorAdapter {

    /**
     * 此拦截器匹配规则
     */
    public static final String[] PATH_URL={"/**"};

    /**
     * 次拦截器忽略匹配
     */
    public static final String[] EXCLUDE_PATH_URL={"/sys/**"};

    @Resource
    private NovelConfig novelConfig;

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response,
                           Object handler,
                           ModelAndView modelAndView) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            SetCommonInfo setCommonInfo = handlerMethod.getMethodAnnotation(SetCommonInfo.class);
            if (setCommonInfo!=null){
                String titleSuffix = novelConfig.getTitle();
                request.setAttribute("titleSuffix",titleSuffix);
                if (setCommonInfo.all()){
                    String keywords = novelConfig.getKeywords();
                    String desc = novelConfig.getDesc();
                    request.setAttribute("keywords",keywords);
                    request.setAttribute("desc",desc);
                }
                // 友情链接
            }

        }
    }
}
